//
//  DiNetEngine.m
//  DooiooOnline
//
//  Created by koki on 14-11-6.
//  Copyright (c) 2014年 dooioo. All rights reserved.
//

#import "HDNetEngine.h"
#import <iflyMSC/iflyMSC.h>

@interface HDNetEngine ()
@property(nonatomic,strong)NSOperationQueue* netQueue;
@property(nonatomic,strong)IFlySpeechSynthesizer *iFlySpeechSynthesizer;
@property(nonatomic,strong)NSTimer* timer;
@property(nonatomic,copy)NSString* tipContent;
@end


@implementation HDNetEngine
@synthesize netQueue;
@synthesize shoppingCount;
@synthesize coachInfo;

static HDNetEngine* instance=nil;
+(HDNetEngine*)sharedInstance
{
    @synchronized(self)
    {
        if(instance==nil)
        {
            instance= [[self alloc]init];
        }
    }
    return instance;
}

- (id)init
{
    self=[super init];
    if(self)
    {
        self.netQueue= [[NSOperationQueue alloc]init];
         self.cdlock=[[NSLock alloc] init];
        
        _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
        _iFlySpeechSynthesizer.delegate = (id)self;
        [_iFlySpeechSynthesizer setParameter:[IFlySpeechConstant TYPE_CLOUD]
                                      forKey:[IFlySpeechConstant ENGINE_TYPE]];
        [_iFlySpeechSynthesizer setParameter:@"50" forKey: [IFlySpeechConstant VOLUME]];
        [_iFlySpeechSynthesizer setParameter:@" tts.pcm" forKey: [IFlySpeechConstant TTS_AUDIO_PATH]];
    }
    return self;
}

-(void)startBroadcast:(NSString*)content
{
    [_iFlySpeechSynthesizer startSpeaking:content];
}

#pragma mark - IFlySpeechSynthesizerDelegate
//结束代理
- (void) onCompleted:(IFlySpeechError *) error
{
    
}
//合成开始
- (void) onSpeakBegin
{
    
}
//合成缓冲进度
- (void) onBufferProgress:(int) progress message:(NSString *)msg
{
    
}
//合成播放进度
- (void) onSpeakProgress:(int) progress
{
    
}

-(void)dealloc
{
    _iFlySpeechSynthesizer=nil;
    if (self.timer!=nil)
    {
        [self.timer invalidate];
        self.timer=nil;
    }
}



- (NSString *)account
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"account"];
}

- (void)setAccount:(NSString *)value
{
    [[NSUserDefaults standardUserDefaults] setValue:value forKey:@"account"];
}

- (NSString *)password
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"password"];
}

- (void)setPassword:(NSString *)value
{
    [[NSUserDefaults standardUserDefaults] setValue:value forKey:@"password"];
}

- (NSString *)address
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"address"];
}

- (void)setAddress:(NSString *)value
{
    [[NSUserDefaults standardUserDefaults] setValue:value forKey:@"address"];
}

-(int)shoppingCount
{
    NSString* keyStr=@"shoppingCount";
    int count=[[[NSUserDefaults standardUserDefaults] objectForKey:keyStr] intValue];
    return count;
}

-(void)setShoppingCount:(int)count
{
    shoppingCount=count;
    NSString* keyStr=@"shoppingCount";
    NSUserDefaults* userDefault=[NSUserDefaults standardUserDefaults];
    [userDefault setObject:[NSNumber numberWithInt:count] forKey:keyStr];
    [userDefault synchronize];
}




-(void)showErrorAlertWithMsg:(NSString*)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

//验证码
-(void)RequestVerifyCodeWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:verifyCodeUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            self.verifyCode=responseDic[@"verify_code"];
            [HDUtil showTipAlertWithMsg:@"验证码已发送到你的手机，请注意查收"];
            [self ResponseVerifyCodeRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}

-(void)requestVerifyCodeSuccuss{}
-(void)ResponseVerifyCodeRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"VerifyCode" forKey:@"VerifyCode"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
    
    if (target && [target respondsToSelector:@selector(requestVerifyCodeSuccuss)])
    {
        [target performSelector:@selector(requestVerifyCodeSuccuss) withObject:nil];
    }
}

//登录
-(void)RequestLoginWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:loginUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseLoginRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseLoginRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"Login" forKey:@"Login"];
    NSDictionary* data=responseDic[@"data"];
    if (data!=nil)
    {
        self.loginState=YES;
        NSDictionary* result=data[@"result"];
        self.coachInfo=[[HDCoachInfo alloc] init];
        coachInfo.coach_no=[HDUtil check:result[@"coach_no"]];
        coachInfo.sex=[HDUtil check:result[@"sex"]];
        coachInfo.user_type=[HDUtil check:result[@"user_type"]];//1教练 2陪练,
        coachInfo.area_code=[HDUtil check:result[@"area_code"]];
        coachInfo.card_no=[HDUtil check:result[@"card_no"]];
        coachInfo.password=[HDUtil check:result[@"password"]];
        coachInfo.user_state=[HDUtil check:result[@"user_state"]];
        coachInfo.xy=[HDUtil check:result[@"xy"]];
        coachInfo.m_id=[HDUtil check:result[@"id"]];
        coachInfo.user_name=[HDUtil check:result[@"user_name"]];
        coachInfo.card_type=[HDUtil check:result[@"card_type"]];
        coachInfo.address=[HDUtil check:result[@"address"]];
        coachInfo.email=[HDUtil check:result[@"email"]];
        coachInfo.coach_licenses=[HDUtil check:result[@"coach_licenses"]];
        coachInfo.stop_result=[HDUtil check:result[@"stop_result"]];
        coachInfo.real_name=[HDUtil check:result[@"real_name"]];
        coachInfo.user_picture=[HDUtil check:result[@"user_picture"]];
        coachInfo.mobile_no=[HDUtil check:result[@"mobile_no"]];
        coachInfo.total_hours=[HDUtil check:result[@"total_hours"]];
        coachInfo.car_type=[HDUtil check:result[@"car_type"]];
        coachInfo.category=[HDUtil check:result[@"category"]];
        coachInfo.cservice=[HDUtil check:result[@"cservice"]];
        coachInfo.cport=[HDUtil check:result[@"cport"]];
        coachInfo.pport=[HDUtil check:result[@"pport"]];
        coachInfo.messageNum=[HDUtil check:result[@"messageNum"]];
        
        self.httpUrl=[NSString stringWithFormat:@"http://%@:%@",coachInfo.cservice,coachInfo.cport];
        self.socketUrl=[NSString stringWithFormat:@"http://%@:%@",coachInfo.cservice,coachInfo.pport];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//注册
-(void)RequestRegisterWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:registerUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
             NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:@"注册成功"];
            [self ResponseRegisterRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseRegisterRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"Register" forKey:@"Register"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//修改密码
-(void)RequestUpdatePasswordWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:updatePasswordUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseUpdatePasswordRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseUpdatePasswordRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"UpdatePassword" forKey:@"UpdatePassword"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//忘记密码
-(void)RequestForgetPasswordWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:forgetPasswordUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:@"密码修改成功"];
            [self ResponseForgetPasswordRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseForgetPasswordRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"forgetPassword" forKey:@"forgetPassword"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//接单
-(void)RequestAcceptOrderWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=acceptOrderUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseAcceptOrderRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseAcceptOrderRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"AcceptOrder" forKey:@"AcceptOrder"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//对指定单操作
-(void)RequestFixedOrderOpWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=targetOrderMadeUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];

    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            [self ResponseFixedOrderOpRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseFixedOrderOpRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"FixedOrderOp" forKey:@"FixedOrderOp"];
    if (responseDic!=nil)
    {
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//获取教练的个人中心
-(void)RequestCoachInfoWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=getCoachInfoUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    [request setTimeOutSeconds:10];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
             NSLog(@"%@",responseDic);
            [self ResponseCoachInfoRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseCoachInfoRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"CoachInfo" forKey:@"CoachInfo"];
    if (responseDic!=nil)
    {
        NSDictionary* data=responseDic[@"data"];
        HDPersonalInfo *personalInfo = [[HDPersonalInfo alloc] init];
        personalInfo.photoUrl = [HDUtil check: data[@"user_picture"]];
        personalInfo.name =[HDUtil check: data[@"real_name"]];
        personalInfo.teachLevel = [HDUtil check: data[@"car_type"]];
        int student_num=[[HDUtil check:data[@"student_num"]] intValue];
        if (student_num>=5000)
        {
           personalInfo.coachLevel = @"金牌教练";
        }
        else if (student_num>=3000)
        {
            personalInfo.coachLevel = @"银牌教练";
        }
        else if (student_num>=1000)
        {
            personalInfo.coachLevel = @"铜牌教练";
        }
        else
        {
            personalInfo.coachLevel = @"铁牌教练";
        }
        
        int category=[[HDUtil check:data[@"category"]] intValue];
        if (category==1)
        {
           personalInfo.subject = @"科目一";
        }
        else if (category==2)
        {
            personalInfo.subject = @"科目二";
        }
        else if (category==3)
        {
            personalInfo.subject = @"科目三";
        }
        else if (category==4)
        {
            personalInfo.subject = @"科目四";
        }
        
        personalInfo.learningOrderCount = [HDUtil check: data[@"xc_number"]];
        personalInfo.practiceOrderCount = [HDUtil check: data[@"lc_number"]];
   
        float level_bf=[[HDUtil check:data[@"level_bf"]] floatValue];
        personalInfo.highOpinionRate = [NSString stringWithFormat:@"%.f%%",level_bf*100];
        
        personalInfo.rank = [HDUtil check: data[@"rowno"]];
        personalInfo.starLevel = [[HDUtil check:data[@"level"]] intValue];
        [userInfo setValue:personalInfo forKey:@"personalInfo"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//获取服务器时间
-(void)RequestServerTimeWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=getServerTimeUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
             NSLog(@"%@",responseDic);
            [self ResponseServerTimeRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseServerTimeRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"ServerTime" forKey:@"ServerTime"];
    if (responseDic!=nil)
    {
        NSNumber* currentTime=responseDic[@"data"];
        [userInfo setValue:currentTime forKey:@"currentTime"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//学车结束，提醒学员确认
-(void)RequestEndConfirmWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=endClassUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            [self ResponseEndConfirmRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseEndConfirmRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"EndConfirm" forKey:@"EndConfirm"];
    if (responseDic!=nil)
    {
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//获取消息列表
-(void)RequestMsgListWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=messageListUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseMsgListRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}

-(void)ResponseMsgListRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"MsgList" forKey:@"MsgList"];
    if (responseDic!=nil)
    {
        NSArray* data=responseDic[@"data"];
        NSMutableArray* msgArray=[[NSMutableArray alloc] initWithCapacity:0];
        if (data!=nil && data.count)
        {
            for (NSDictionary* item in data)
            {
                HDMessageInfo *message = [[HDMessageInfo alloc] init];
                message.msgContent = item[@"content"];
                NSDate* date=[NSDate dateWithTimeIntervalSince1970:[item[@"create_time"] longLongValue]/1000];;
                message.time = [HDUtil formatter:@"YYYY-MM-dd HH:mm" FromeDate:date];
                message.isUnread = ([item[@"state"] intValue]==0);
                [msgArray addObject:message];
            }
        }
        [userInfo setValue:msgArray forKey:@"data"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//清除消息
-(void)RequestDeleteMsgListWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=removeMessageUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseDeleteMsgListRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseDeleteMsgListRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"DeleteMsgList" forKey:@"DeleteMsgList"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//问题反馈
-(void)RequestAskCompWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=saveAskCompUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


//个人中心：学车和练车订单列表
-(void)RequestCoachOrderWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=coachOrderUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseCoachOrderRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseCoachOrderRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"CoachOrder" forKey:@"CoachOrder"];
    NSDictionary* dataDic=responseDic[@"data"];
    if (dataDic!=nil)
    {
        NSArray* orderList=dataDic[@"orderList"];
        NSMutableArray* dataArray=[[NSMutableArray alloc] initWithCapacity:0];
        
        for (NSDictionary* item in orderList)
        {
            HDCustomerInfo *customerInfo = [[HDCustomerInfo alloc] init];
            //添加解析
            int category=[[HDUtil check:item[@"category"]] intValue];
            NSArray* array=@[@"无",@"科目一",@"科目二",@"科目三",@"科目四"];
            customerInfo.subject = array[category];
            customerInfo.photoUrl = [HDUtil check:item[@"user_picture"]];
            customerInfo.stationTime = [HDUtil check:item[@"total_hours"]];
            long total=(long)llabs([customerInfo.stationTime longLongValue]);
            customerInfo.stationTime=[NSString stringWithFormat:@"%ld小时",total/3600];
            customerInfo.name=[HDUtil check:item[@"student_name"]];
            customerInfo.type=[HDUtil check:item[@"car_type"]];
            customerInfo.remark = [HDUtil check:item[@"remark"]];
            customerInfo.price = [HDUtil check:item[@"order_money"]];
            customerInfo.practicePosition = [HDUtil check:item[@"training_field"]];
            customerInfo.needCompany = [item[@"is_pick"] boolValue];
            customerInfo.order_id=[HDUtil check:item[@"id"]];
            customerInfo.location=[HDUtil check:item[@"address"]];
            array=@[@"无",@"待支付",@"退单申请中",@"已支付",@"已抢单",@"已拒绝",@"进行中",@"已完成",@"已退单完成",@"退单已拒绝",@"待确认"];
            int state=[item[@"state"] intValue];
            customerInfo.orderStatus = array[state];
            
            int orderType=[item[@"order_type"] intValue];
            customerInfo.orderType=(orderType==1)?HDOrderTypeLearn:HDOrderTypePractice;
            customerInfo.phoneNumber=[HDUtil check:item[@"mobile_no"]];
            customerInfo.startTime=item[@"start_time"];
            customerInfo.endTime=item[@"end_time"];
            customerInfo.learnTime = [HDUtil check:item[@"order_hours"]];
            
            NSString* xy=[HDUtil check:item[@"xy"]];
            xy=[xy stringByReplacingOccurrencesOfString:@"(" withString:@""];
            xy=[xy stringByReplacingOccurrencesOfString:@")" withString:@""];
            array=[xy componentsSeparatedByString:@","];
            if(array.count>1)
            {
                customerInfo.latitude=[array[0] floatValue];
                customerInfo.longitude=[array[1] floatValue];
            }
            [dataArray addObject:customerInfo];
        }
        [userInfo setValue:dataArray forKey:@"dataArray"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//我的收入当前余额
-(void)RequestBalanceWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{

    NSString* strUrl=balanceUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseBalanceRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseBalanceRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"Balance" forKey:@"Balance"];
    NSString* incomeStr=responseDic[@"data"];
    [userInfo setValue:incomeStr forKey:@"incomeStr"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//我的收支账单明细
-(void)RequestFlowWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=flowUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseFlowRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseFlowRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"Flow" forKey:@"Flow"];
    
       NSArray* data=responseDic[@"data"];
        NSMutableArray* dataArray=[[NSMutableArray alloc] initWithCapacity:0];
        
        for (NSDictionary* item in data)
        {
            HDIncomeInfo *incomeInfo = [[HDIncomeInfo alloc] init];
           
            incomeInfo.detailInfo = [HDUtil check:item[@"content"]];
            incomeInfo.number=[HDUtil check:item[@"money"]];
            incomeInfo.type=[HDUtil check:item[@"type"]];
            incomeInfo.week = [HDUtil check:item[@"week"]];
            incomeInfo.date = [HDUtil check:item[@"yearMonth"]];
            [dataArray addObject:incomeInfo];
        }
        [userInfo setValue:dataArray forKey:@"dataArray"];
        
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//提现申请单
-(void)RequestWithdrawWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=coachTxAppUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseWithdrawRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseWithdrawRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"Withdraw" forKey:@"Withdraw"];
    [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//学车练车:指定单、待上课列表请求
-(void)RequestOrderListWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
 NSLog(@"%@",params);
    NSString* strUrl=orderListUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseOrderListRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseOrderListRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"OrderList" forKey:@"OrderList"];
    int count = [[responseDic objectForKey:@"count"] intValue];
    int totalPage = count%20?count/20:(count/20+1);
    [userInfo setValue:[NSNumber numberWithInt:totalPage] forKey:@"totalPage"];
    NSDictionary* dataDic=responseDic[@"data"];
    if (dataDic!=nil)
    {
        NSArray* orderList=dataDic[@"orderList"];
        NSMutableArray* dataArray=[[NSMutableArray alloc] initWithCapacity:0];
        
        for (NSDictionary* item in orderList)
        {
            HDCustomerInfo *customerInfo = [[HDCustomerInfo alloc] init];
            //添加解析
            int category=[[HDUtil check:item[@"category"]] intValue];
            NSArray* array=@[@"无",@"科目一",@"科目二",@"科目三",@"科目四"];
            customerInfo.subject = array[category];
            customerInfo.photoUrl = [HDUtil check:item[@"user_picture"]];
            customerInfo.stationTime = [HDUtil check:item[@"total_hours"]];
            long total=(long)llabs([customerInfo.stationTime longLongValue]);
            customerInfo.stationTime=[NSString stringWithFormat:@"%ld小时",total/3600];
            customerInfo.name=[HDUtil check:item[@"student_name"]];
            customerInfo.type=[HDUtil check:item[@"car_type"]];
            customerInfo.remark = [HDUtil check:item[@"remark"]];
            customerInfo.price = [HDUtil check:item[@"order_money"]];
            customerInfo.practicePosition = [HDUtil check:item[@"training_field"]];
            customerInfo.needCompany = [item[@"is_pick"] boolValue];
            customerInfo.order_id=[HDUtil check:item[@"id"]];
            customerInfo.location=[HDUtil check:item[@"address"]];
            array=@[@"无",@"待支付",@"退单申请中",@"已支付",@"已抢单",@"已拒绝",@"进行中",@"已完成",@"已退单完成",@"退单已拒绝",@"待确认"];
            int state=[item[@"state"] intValue];
            customerInfo.orderStatus = array[state];
            
            int orderType=[item[@"order_type"] intValue];
            customerInfo.orderType=(orderType==1)?HDOrderTypeLearn:HDOrderTypePractice;
            customerInfo.phoneNumber=[HDUtil check:item[@"mobile_no"]];
            customerInfo.startTime=item[@"start_time"];
            customerInfo.endTime=item[@"end_time"];
            customerInfo.learnTime = [HDUtil check:item[@"order_hours"]];
            
            NSString* xy=[HDUtil check:item[@"xy"]];
            xy=[xy stringByReplacingOccurrencesOfString:@"(" withString:@""];
            xy=[xy stringByReplacingOccurrencesOfString:@")" withString:@""];
            array=[xy componentsSeparatedByString:@","];
            if(array.count>1)
            {
                customerInfo.latitude=[array[0] floatValue];
                customerInfo.longitude=[array[1] floatValue];
            }
            [dataArray addObject:customerInfo];
        }
        [userInfo setValue:dataArray forKey:@"dataArray"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//对指定单操作:拒绝，接单
-(void)RequestTargetOrderOpWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    
    NSString* strUrl=targetOrderMadeUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseTargetOrderOpRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseTargetOrderOpRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"TargetOrderOp" forKey:@"TargetOrderOp"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//上课状态
-(void)RequestLessonStatusWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=lessonStatusUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200||[responseDic[@"code"] intValue]==0 ||[responseDic[@"code"] intValue]==-1||[responseDic[@"code"] intValue]==-2)
        {
            NSLog(@"%@",responseDic);
            [self ResponseLessonStatusRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseLessonStatusRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"LessonStatus" forKey:@"LessonStatus"];
    NSNumber* startTime=responseDic[@"data"];
    [userInfo setValue:startTime forKey:@"startTime"];
    [userInfo setValue:responseDic[@"code"] forKey:@"code"];
    [userInfo setValue:responseDic[@"message"] forKey:@"message"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//开始上课
-(void)RequestStartLessonWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=startLessonUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseStartLessonRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseStartLessonRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"StartLesson" forKey:@"StartLesson"];
    NSNumber* startTime=responseDic[@"data"];
    [userInfo setValue:startTime forKey:@"startTime"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//上课结束
-(void)RequestEndLessonWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSString* strUrl=endLessonUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseEndLessonRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseEndLessonRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"EndLesson" forKey:@"EndLesson"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//教练位置上传
-(void)RequestUpdateSiteWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{

    NSString* strUrl=updateXYUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [HDUtil showTipAlertWithMsg:responseDic[@"message"]];
            [self ResponseUpdateSiteRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseUpdateSiteRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"UpdateSite" forKey:@"UpdateSite"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}

//安全码接口
-(void)RequestSecurityCodeWithTarget:(id) target hudView:(UIView *)hudView
{
    NSString* strUrl=securityCodeUrl;
    strUrl=[strUrl stringByReplacingOccurrencesOfString:hostUrl withString:self.httpUrl];
    NSURL *url=[NSURL URLWithString:strUrl];
    
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseSecurityCodeRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
        
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}


-(void)ResponseSecurityCodeRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:[responseDic objectForKey:@""] forKey:@""];
    [userInfo setValue:responseDic[@"safe_code"] forKey:@"SecurityCode"];
    [[NSNotificationCenter defaultCenter] postNotificationName:HDViewControllerUpdateNotification object:self userInfo:userInfo];
}


//待上班
-(void)RequesWorkWithTarget:(id) target hudView:(UIView *)hudView Data:(NSDictionary *)params
{
    NSURL *url=[NSURL URLWithString:workUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    
    [request setCompletionBlock:^{
        [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            NSLog(@"%@",responseDic);
            [self ResponseWorkRequest:responseDic target:target];
        }
        else
        {
            [self showErrorAlertWithMsg:responseDic[@"message"]];
        }
    }];
    
    [request setFailedBlock:^
     {
         [SXProgressHUD hideAllHUDsForView:hudView animated:YES];
         [self showErrorAlertWithMsg:@"请求失败，请稍后再试"];
     }];
    
    [SXProgressHUD showHUDAddedTo:hudView animated:YES];
    [self.netQueue addOperation:request];
}

-(void)requestWorkInfoSuccuss:(HDWorkInfo*)workInfo{}

-(void)ResponseWorkRequest:(NSDictionary*)responseDic target:(id)target
{
    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObjectsAndKeys:target,Receiver, nil];
    [userInfo setValue:@"workInfo" forKey:@"workInfo"];
    if (responseDic!=nil)
    {
        HDWorkInfo* workInfo=[[HDWorkInfo alloc] init];
        workInfo.week=[HDUtil check:responseDic[@"week"]];
        workInfo.user_count=[HDUtil check:responseDic[@"user_count"]];
        workInfo.order_count=[HDUtil check:responseDic[@"order_count"]];
        workInfo.class_count=[HDUtil check:responseDic[@"class_count"]];
        
        if (target && [target respondsToSelector:@selector(requestWorkInfoSuccuss:)])
        {
            [target performSelector:@selector(requestWorkInfoSuccuss:) withObject:workInfo];
        }
    }
    
}



//心跳
-(void)RequestHeartBeat
{
    NSDictionary *params=@{@"id":self.coachInfo.m_id};
    NSURL *url=[NSURL URLWithString:heartbeatUrl];
    ASIFormDataRequest*request=[ASIFormDataRequest requestWithURL:url];
    ASIFormDataRequest* weakRequest = request;
    [request setRequestMethod:@"POST"];
    [request setPostDictionary:params];
    [request setCompletionBlock:^{
        NSData *responseData= weakRequest.responseData;
        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        if([responseDic[@"code"] intValue]==200)
        {
            if(responseDic[@"data"]!=nil)
            {
                NSArray* array=responseDic[@"data"];
                if (array.count)
                {
                    NSString* content=[NSString string];
                    for(NSDictionary* item in array)
                    {
                        content=[content stringByAppendingString:[HDUtil check:item[@"content"]]];
                    }
                    if (content.length && ![self.tipContent isEqual:content])
                    {
                        self.tipContent=content;
                        [netEngine startBroadcast:self.tipContent];
                    }
                }
            }
        }
    }];
    [self.netQueue addOperation:request];
}

-(void)setLoginState:(BOOL)loginState
{
    _loginState=loginState;
    
    if (self.timer!=nil)
    {
        [self.timer invalidate];
        self.timer=nil;
    }
    
    if (loginState)
    {
        self.timer=[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(RequestHeartBeat) userInfo:nil repeats:YES];
    }
    
}

@end



